<?php

namespace common\widgets\grid;

use Yii;
use common\helpers\Html;

/**
 * ActionColumn is a column for the [[GridView]] widget that displays buttons for viewing and manipulating the items.
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class ActionColumn extends \yii\grid\ActionColumn {

    /**
     * @inheritdoc
     */
    public $header = '操作';

    /**
     * @inheritdoc
     */
    public $headerOptions = ['class' => 'action-column text-right'];

    /**
     * @inheritdoc
     */
    public $contentOptions = ['class' => 'button-column'];

    /**
     * @inheritdoc
     */
    public $buttonOptions = ['class' => 'btn btn-sm btn-outline'];

    /**
     * @inheritdoc
     */
    protected function initDefaultButtons() {
        $this->initDefaultButton('view', 'blue-hoki');
        $this->initDefaultButton('update', 'yellow-gold');
        $this->initDefaultButton('delete', 'dark', [
            'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
            'data-method' => 'post',
        ]);
    }

    /**
     * @inheritdoc
     */
    protected function initDefaultButton($name, $className, $additionalOptions = []) {
        if (!isset($this->buttons[$name]) && strpos($this->template, '{' . $name . '}') !== false) {
            $this->buttons[$name] = function ($url, $model, $key) use ($name, $className, $additionalOptions) {
                switch ($name) {
                    case 'view':
                        $title = Yii::t('yii', 'View');
                        break;
                    case 'update':
                        $title = '修改';
                        break;
                    case 'delete':
                        $title = Yii::t('yii', 'Delete');
                        break;
                    default:
                        $title = ucfirst($name);
                }
                $options = array_merge([
                    'title' => $title,
                    'aria-label' => $title,
                    'data-pjax' => 0,
                ], $additionalOptions, $this->buttonOptions);
                Html::addCssClass($options, $className);
                return Html::a($title, $url, $options);
            };
        }
    }

    /**
     * @inheritdoc
     */
    protected function renderDataCellContent($model, $key, $index) {
        return preg_replace_callback('/\\{([^}]+)\\}/', function ($matches) use ($model, $key, $index) {
            $matchData = explode(':', $matches[1] . ':');
            list($name, $title) = $matchData;
            $className = isset($matchData[2]) && $matchData[2] ? $matchData[2] : 'blue-hoki';
            if ($title == '') {
                $title = Yii::t('yii', ucfirst($name));
            }
            if (isset($this->visibleButtons[$name])) {
                $isVisible = $this->visibleButtons[$name] instanceof \Closure ? call_user_func($this->visibleButtons[$name], $model, $key, $index) : $this->visibleButtons[$name];
            } else {
                $isVisible = true;
            }

            if (!$isVisible) {
                return '';
            }

            if (isset($this->buttons[$name])) {
                $url = $this->createUrl($name, $model, $key, $index);
                return call_user_func($this->buttons[$name], $url, $model, $key);
            } else {
                $url = $this->createUrl($name, $model, $key, $index);
                $options = array_merge([
                    'title' => $title,
                    'aria-label' => $title,
                    'data-pjax' => 0,
                ], $this->buttonOptions);
                if ($className === 'modal') {
                    return Html::ajaxModal($title, $url, $options);
                }
                Html::addCssClass($options, $className);
                return Html::a($title, $url, $options);
            }
        }, $this->template);
    }

}
